package com.forezp.web;

import com.forezp.entity.User;
import com.forezp.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by forezp on 2017/5/26.
 */
@RequestMapping("/user")
@RestController
@Api(value = "用户相关")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation(value = "用户列表", notes = "用户列表", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    public List<User> getUsers() {
        List<User> users = userService.findAll();
        User user = userService.findUserByName("bug");
        User user2 = userService.findByUserByPassword("123456789");
        return users;
    }

    @ApiOperation(value = "创建用户", notes = "创建用户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/create/", method = RequestMethod.POST)
    public User postUser(
            @ApiParam(name = "用户对象", value = "用户对象节点", required = true)
            @RequestBody User user) {
        return userService.saveUser(user);

    }

    @ApiOperation(value = "获用户细信息", notes = "根据url的id来获取详细信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public User getUser(@ApiParam(name = "id", value = "用户id", required = true) @PathVariable Long id) {
        return userService.findUserById(id);
    }

    @ApiOperation(value = "更新信息", notes = "根据url的id来指定更新用户信息", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    public User putUser(@ApiParam(name = "id", value = "用户id", required = true) @PathVariable Long id,
                        @ApiParam(name = "用户对象", value = "用户对象节点", required = true) @RequestBody User user) {
        User user1 = new User();
        user1.setUsername(user.getUsername());
        user1.setPassword(user.getPassword());
        user1.setId(id);
        return userService.updateUser(user1);

    }

    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除用户", httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public String deleteUser(
            @ApiParam(name = "id", value = "用户id", required = true)
            @PathVariable Long id) {
        userService.deleteUser(id);
        return "success";
    }

    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String jsonTest() {
        return " hi you!";
    }


    @ApiOperation(value = "通过用户名获得用户", notes = "根据用户名精确查找指定用户", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/getUserByUserName/{username}")
    public User getUser(
            @ApiParam(name = "用户名", value = "用户名称", required = true)
            @PathVariable("username") String username) {
        return userService.findUserByName(username);
    }
}
